/*
 * Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.tsunami.plugins.detectors.directorytraversal.genericpathtraversaldetector;

import com.google.auto.value.AutoValue;
import com.google.tsunami.common.net.http.HttpRequest;
import com.google.tsunami.proto.NetworkService;
import java.util.Comparator;

/** An HttpRequest containing a payload that tries to exploit a potential vulnerability. */
@AutoValue
abstract class PotentialExploit {
  abstract NetworkService networkService();

  /** Finalized HttpRequest containing the crafted payload. */
  abstract HttpRequest request();

  abstract String payload();

  abstract Priority priority();

  static PotentialExploit create(
      NetworkService networkService, HttpRequest request, String payload, Priority priority) {
    return new AutoValue_PotentialExploit(networkService, request, payload, priority);
  }

  @Override
  public final boolean equals(Object o) {
    if (o == null) {
      return false;
    }
    if (!(o instanceof PotentialExploit)) {
      return false;
    }
    if (this == o) {
      return true;
    }
    PotentialExploit that = (PotentialExploit) o;
    return this.request().equals(that.request());
  }

  @Override
  public final int hashCode() {
    return this.request().hashCode();
  }

  @Override
  public final String toString() {
    return String.format(
        "PotentialExploit{request=%s, payload=%s}", this.request(), this.payload());
  }

  enum Priority {
    LOW(0),
    MEDIUM(1),
    HIGH(2);

    private final int value;

    Priority(int value) {
      this.value = value;
    }

    public static final Comparator<Priority> COMPARATOR =
        (Priority p1, Priority p2) -> p2.value - p1.value;
  }
}
